<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref } from 'vue';
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicForm, useForm } from '@/components/Form/index';
import { accountFormSchema } from './account.data';
import { addSysUser, editSysUser } from '@/api/sys/sysUserManage';
import { useMessage } from '@/hooks/web/useMessage';
import { useI18n } from '@/hooks/web/useI18n';
export default defineComponent({
  name: 'AccountModal',
  components: { BasicModal, BasicForm },
  emits: ['success', 'register'],
  setup(_, { emit }) {
    let modalType = ref('create');
    const { t } = useI18n();
    const [registerForm, { setFieldsValue, resetFields, validate, setProps }] =
      useForm({
        labelWidth: 100,
        baseColProps: { span: 24 },
        schemas: accountFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 23,
        },
      });

    const [registerModal, { setModalProps, closeModal }] = useModalInner(
      async (data) => {
        resetFields();
        modalType.value = data?.type;
        setProps({ disabled: false });
        if (unref(modalType) === 'watch') {
          // 隐藏取消 确认按钮
          setModalProps({ showOkBtn: false, showCancelBtn: false });
          setProps({ disabled: true });
          setFieldsValue({
            ...data.record,
            roleId: data.record.roleId ? data.record.roleId.split(',') : [],
            orgId: data.record.orgId ? data.record.orgId.split(',') : [],
          });
          return;
        }
        if (unref(modalType) === 'edit') {
          setFieldsValue({
            ...data.record,
            roleId: data.record.roleId ? data.record.roleId.split(',') : [],
            orgId: data.record.orgId ? data.record.orgId.split(',') : [],
          });
        }
        // 非查看 显示取消 确认按钮
        setModalProps({ showOkBtn: true, showCancelBtn: true });
      }
    );

    const getTitle = computed(() => {
      if (unref(modalType) === 'edit') {
        return t('编辑');
      } else if (unref(modalType) === 'create') {
        return t('新增');
      } else {
        return t('查看');
      }
    });
    const { createMessage } = useMessage();
    async function handleSubmit() {
      try {
        const values = await validate();
        values.orgId = values.orgId.join(',');
        values.roleId = values.roleId.join(',');
        setModalProps({ confirmLoading: true });
        // TODO custom api
        console.log(values);
        unref(modalType) === 'create'
          ? await addSysUser(values)
          : await editSysUser(values);
        if (unref(modalType) === 'add') {
          createMessage.success(t('新增成功'));
        } else {
          createMessage.success(t('修改成功'));
        }
        closeModal();
        emit('success');
      } finally {
        setModalProps({ confirmLoading: false });
      }
    }

    return { registerModal, registerForm, getTitle, t, handleSubmit };
  },
});
</script>
